﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_FlightEdit : System.Web.UI.Page
{
    protected int id = Helper.getQuery("flightID");
    FlightBusiness fb = new FlightBusiness();
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) { 
        FlightEntities fe = fb.getFlight(id);
        bindDDLPlane();
        bindDDLKM();
        binDDLPanel();
        if (id > 0)
        {
            btnAddNew.Text = "Sửa";
            Label1.Text=id.ToString();
            txtDi.Text = fe._departure;
            txtDen.Text = fe._arrival;
            txtStartTime.Text = fe._startTime;
            txtEndTime.Text = fe._endTime;
            txtNgayDi.Text = fe._departureDate;
            txtNgayDen.Text = fe._arrivalDate;
            txtGia.Text = fe._price.ToString();
            txtMiles.Text = fe._miles.ToString();
        }
        else
            btnAddNew.Text = "Thêm";
        }
    }
    public void bindDDLPlane()
    {
        PlaneBusiness pb = new PlaneBusiness();
        ddlPlane.DataSource = pb.getAllPlane();
        ddlPlane.DataTextField = "planeName";
        ddlPlane.DataValueField = "planeID";
        ddlPlane.DataBind();
    }
    public void bindDDLKM()
    {
        PromotionsBuisness prb = new PromotionsBuisness();
        ddlKM.DataSource = prb.getAllPromotions();
        ddlKM.DataTextField = "promotionsName";
        ddlKM.DataValueField = "promotionsID";
        ddlKM.DataBind();
    }
    public void binDDLPanel()
    {
        PanelBusiness pb = new PanelBusiness();
        ddlPanelEntities.DataSource = pb.getAllPanel();
        ddlPanelEntities.DataTextField = "pannelHeader";
        ddlPanelEntities.DataValueField = "panelID";
        ddlPanelEntities.DataBind();
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        if (btnAddNew.Text == "Sửa")
        {
               string[] values = { id.ToString(), ddlPlane.SelectedValue, txtDi.Text, txtDen.Text,
                                  txtStartTime.Text,txtEndTime.Text,txtNgayDi.Text,txtNgayDen.Text,ddlKM.SelectedValue,
                                  txtGia.Text,txtMiles.Text,"0"};
               if (fb.Edit(values))
                   Response.Redirect("Flight.aspx");
               else
                   Label1.Text = "Chưa sửa được";
           
        }
        else
        {
            string[] values = {  ddlPlane.SelectedValue, txtDi.Text, txtDen.Text,
                                  txtStartTime.Text,txtEndTime.Text,txtNgayDi.Text,txtNgayDen.Text,ddlKM.SelectedValue,
                                  txtGia.Text,txtMiles.Text,CheckBox1.Checked?"1":"0"};
             if (fb.Insert(values))
                Response.Redirect("Flight.aspx");
            else
                Label1.Text = "Chưa thêm được";
        }
    }
}